• File: REEFT_date_convert.php
  • Full Path: C:/htdocs/reeft_gps_test/css/include/REEFT_date_convert.php
  • Date Modified: 04/30/2025 7:56 AM
  • File size: 14.66 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php
//======================================================================================
//
// Function: Convert date and set/get timezone
//
// Programmer: JKJ
// Date      : 2022-03-21
//
// Copyright Reeft A/S (c) - 2022
//======================================================================================

//======================================================================================
//
//	Time zones	: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
//	date_format	: https://www.w3schools.com/php/func_date_date_format.asp
//
//======================================================================================

//======================================================================================
// Timezone is coming from config/config.php
//======================================================================================
//
//	REEFT_local_to_utc			: Convert local date to UTC
//	REEFT_utc_to_local			: Convert UTC date to local
//	REEFT_current_local			: Get local data based on current time zone
//	REEFT_current_utc			: Get current UTC date
//	REEFT_current_timezone_set 	: Set current time zone - default is Europe/Copenhagen
//	REEFT_current_timezone_get 	: Get current time zone
//	REEFT_format_date 			: Format a data to another date format - input i ISO timestamp
//								  output is just a date in some format
//								  Valid formats - if an invalid format DFT_DATE_DISPLAY_FORMAT
//								  from config.php is used
// 									*YMD 	= 2022-12-31
// 									*DMY 	= 31-12-2022
// 									*USA 	= 12/31/2022
// 									*ISO 	= 2022-12-31
// 									*EUR 	= 31.12.2022
// 									*JIS 	= 2022-12-31
// 									*LONGJUL = 2022/365
// 									*PRETTY 	= 1. Marts 2022
// 									*PRETTY1	= 1. Mar. 2022
// 									*PRETTY2	= Mandag, 1. Marts 2022
//
//======================================================================================

	//@$move_on_up = $move_on_up;
	//include $move_on_up . "config/config.php";

//======================================================================================
// Check for variables
//======================================================================================

	$isset = isset($DFT_DATE_DISPLAY_FORMAT);
	if ( $isset <> true ) {
		$DFT_DATE_DISPLAY_FORMAT = '*ISO';
	}

	$isset = isset($DFT_CURRENT_TIMEZONE);
	if ( $isset <> true ) {
		$DFT_CURRENT_TIMEZONE 		= 'Europe/Copenhagen';
	}

//======================================================================================
// Function: Convert from UTC to Local
//======================================================================================
function REEFT_utc_to_local( $dateInUTC ) {

	// Check if date is valid
	$returnCode = isValidDate($dateInUTC);
	if ( $returnCode == 1 ) {
		return '';
		// return '0001-01-01 00:00:00';
		// return '*ERROR';
	}

	if ( $dateInUTC <> '' ) {
		REEFT_current_timezone_set();

		$time = strtotime($dateInUTC.' UTC');
		$dateTimeLocal = date("Y-m-d H:i:s", $time);
	} else {
		$dateTimeLocal = '';
	}

	return $dateTimeLocal;

}

//======================================================================================
// Function: Convert from Local to UTC
//======================================================================================
function REEFT_local_to_utc( $dateInLocal ) {


	// Check if date is valid
	$returnCode = isValidDate($dateInLocal);
	if ( $returnCode == 1 ) {
		return '';
		//return '*ERROR';
	}

	if ( $dateInLocal <> '' ) {
		REEFT_current_timezone_set();

		$dateTime = $dateInLocal;
		$newDateTime = new DateTime($dateTime);
		$newDateTime->setTimezone(new DateTimeZone("UTC"));
		$dateTimeUTC = $newDateTime->format("Y-m-d H:i:s");
	
	} else {
		$dateTimeUTC = '';
	}

	return $dateTimeUTC;

}

//======================================================================================
// Function: Get current date in UTC
//======================================================================================
function REEFT_current_utc() {

	REEFT_current_timezone_set();

	$dateTime = date("Y-m-d H:i:s");
	$newDateTime = new DateTime($dateTime);
	$newDateTime->setTimezone(new DateTimeZone("UTC"));
	$dateInUTC = $newDateTime->format("Y-m-d H:i:s");

	return $dateInUTC;

}

//======================================================================================
// Function: Get current date in local
//======================================================================================
function REEFT_current_local()
{
	REEFT_current_timezone_set();

	$dateTime = date("Y-m-d H:i:s");
	$newDateTime = new DateTime($dateTime);
	//$newDateTime->setTimezone(new DateTimeZone($currentTimeZone));
	$dateInLocal = $newDateTime->format("Y-m-d H:i:s");

	return $dateInLocal;

}

//======================================================================================
// Function: Get current time zone
//======================================================================================
function REEFT_current_timezone_get()
{

    $currentTimeZone = date_default_timezone_get();

	return $currentTimeZone;

}

//======================================================================================
// Function: Set current time zone
// Default: Europe/Copenhagen
//======================================================================================
function REEFT_current_timezone_set( $timeZone='' )
{

	global $DFT_CURRENT_TIMEZONE;

	// No timezone passed to function, use DK
	if ( $timeZone == '' ) {
		// $timeZone = 'Europe/London';
		//$timeZone = 'Africa/Mogadishu';
		$timeZone = $DFT_CURRENT_TIMEZONE;
	}

	date_default_timezone_set( $timeZone );

	return $timeZone;

}

//======================================================================================
// Function: Convert to config data format
//======================================================================================
// *YMD 	= 2022-12-31
// *DMY 	= 31-12-2022
// *USA 	= 12/31/2022
// *ISO 	= 2022-12-31
// *EUR 	= 31.12.2022
// *JIS 	= 2022-12-31
// *LONGJUL = 2022/365
// *PRETTY 	= 1. Marts 2022
// *PRETTY1	= 1. Mar. 2022
// *PRETTY2	= Mandag, 1. Marts 2022
//======================================================================================
function REEFT_format_date( $some_date, $output_date_format = '*ISO' )
{

	// Check if date is valid
	$returnCode = isValidDate($some_date);
	if ( $returnCode == 1 ) {
		return '';
		//return '*ERROR';
	}


	//======================================================================================
	// Set globals
	//======================================================================================
	global $DFT_LANGUAGE ;
	global $DFT_DATE_DISPLAY_FORMAT;
	global $datahub_month;
	global $datahub_month_short;
	global $datahub_day_name;
	global $datahub_day_name_ISO;

	$date_format_check = 'N';

	if (	$output_date_format == '*YMD'
		or 	$output_date_format == '*DMY'
		or 	$output_date_format == '*USA'
		or 	$output_date_format == '*ISO'
		or 	$output_date_format == '*EUR'
		or 	$output_date_format == '*JIS'
		or 	$output_date_format == '*PRETTY'
		or 	$output_date_format == '*PRETTY1'
		or 	$output_date_format == '*PRETTY2'
		or 	$output_date_format == '*LONGJUL'
		)
		{
			$date_format_check = 'Y';
		}

		if ( $date_format_check == 'N' ) {
			$output_date_format = $DFT_DATE_DISPLAY_FORMAT;
		}

	// Create date object
	$some_date_object = date_create($some_date);


	// Convert date
	if ( $output_date_format == '*ISO' ) {
		$some_date = date_format($some_date_object,'Y-m-d H:i:s');
	}

	if ( $output_date_format == '*YMD' ) {
		$some_date = date_format($some_date_object,'Y-m-d H:i:s');
	}

	if ( $output_date_format == '*DMY' ) {
		$some_date = date_format($some_date_object,'d-m-Y H:i:s');
	}

	if ( $output_date_format == '*USA' ) {
		$some_date = date_format($some_date_object,'m/d/Y h:i:s A');
	}

	if ( $output_date_format == '*EUR' ) {
		$some_date = date_format($some_date_object,'d.m.Y H:i:s');
	}

	if ( $output_date_format == '*JIS' ) {
		$some_date = date_format($some_date_object,'Y-m-d H:i:s');
	}

	if ( $output_date_format == '*LONGJUL' ) {
		$wrk_day 	= date_format($some_date_object,'z') + 1;
		$wrk_year	= date_format($some_date_object,'Y');
		$some_date = $wrk_year . '/' . $wrk_day . ' ' . date_format($some_date_object,'H:i:s');
	}

	if ( $output_date_format == '*PRETTY' ) {

		$wrk_month = date_format($some_date_object,'n');
		$month_locale = $datahub_month[$wrk_month];

		$some_date = date_format($some_date_object,'j') . '. ' . $month_locale . ' ' . date_format($some_date_object,'Y') . ' ' . date_format($some_date_object,'H:i:s');
	}

	if ( $output_date_format == '*PRETTY1' ) {

		$wrk_month = date_format($some_date_object,'n');
		$month_locale = $datahub_month_short[$wrk_month];

		$some_date = date_format($some_date_object,'j') . '. ' . $month_locale . '. ' . date_format($some_date_object,'Y') . ' ' . date_format($some_date_object,'H:i:s');
	}

	if ( $output_date_format == '*PRETTY2' ) {

		$wrk_month = date_format($some_date_object,'n');
		$month_locale = $datahub_month[$wrk_month];


		$wrk_day = date_format($some_date_object,'N');
		//$wrk_day = date_format($some_date_object,'w');

		$day_locale = $datahub_day_name_ISO[$wrk_day];

		$some_date = $day_locale . ', ' . date_format($some_date_object,'j') . '. ' . $month_locale . ' ' . date_format($some_date_object,'Y') . ' ' . date_format($some_date_object,'H:i:s');
	}



	return $some_date;

}

//======================================================================================
// Function: Calculate new date
//======================================================================================
function REEFT_calc_date( $input_date, $repeat_value = '*DAY', $repeat_number = 1 )
{

		global $DFT_EVENT_ADJUST_WEEKEND_ON_REPEAT;

		$output_date = '';

		// Check repeat_value
		$i_am_ok = 'N';

		if 	(  $repeat_value == '*MINUTE'
			or $repeat_value == '*HOUR'
			or $repeat_value == '*DAY'
			or $repeat_value == '*WEEK'
			or $repeat_value == '*MONTH'
			or $repeat_value == '*YEAR'
			)
			{
				$i_am_ok = 'Y';
			}


		// If not valid, set default to *DAY
		if ( $i_am_ok == 'N' ) {
			$repeat_value = '*DAY';
		}

		// Check repeat_number, cannot be zero - default is 1
		if ( $repeat_number < 1 ) {
			$repeat_number = 1;
		}

		// Add
		if ( $repeat_value == '*MINUTE' ) {
			$output_date = date('Y-m-d H:i:s', strtotime($input_date . ' +' . $repeat_number . 'minute'));
		}

		if ( $repeat_value == '*HOUR' ) {
			$output_date = date('Y-m-d H:i:s', strtotime($input_date . ' +' . $repeat_number . 'hour'));
		}

		if ( $repeat_value == '*DAY' ) {
			$output_date = date('Y-m-d H:i:s', strtotime($input_date . ' +' . $repeat_number . 'day'));
		}

		if ( $repeat_value == '*WEEK' ) {
			$output_date = date('Y-m-d H:i:s', strtotime($input_date . ' +' . $repeat_number . 'week'));
		}

		if ( $repeat_value == '*MONTH' ) {
			$output_date = date('Y-m-d H:i:s', strtotime($input_date . ' +' . $repeat_number . 'month'));
		}
		if ( $repeat_value == '*YEAR' ) {
			$output_date = date('Y-m-d H:i:s', strtotime($input_date . ' +' . $repeat_number . 'year'));
		}

		// $tmpRow["input_date"] 	= $input_date;
		// $tmpRow["output_date"] 	= $output_date;
		if ( $output_date == '' ) {
			$output_date = $input_date;
		}

		// Check of Saturday/sunday
		if ( $DFT_EVENT_ADJUST_WEEKEND_ON_REPEAT == 'Y' ) {
			
			$day_number_ISO = REEFT_get_day_number_ISO( $output_date );			
			
			// Saturday
			if ( $day_number_ISO == 6 ) {
				$output_date = REEFT_calc_date( $output_date, '*DAY', 2 );
			}
			// Sunday
			if ( $day_number_ISO == 7 ) {
				$output_date = REEFT_calc_date( $output_date, '*DAY', 1 );
			}			
			
		}


		return $output_date;
}

//======================================================================================
// Function: Get day number
//======================================================================================
function REEFT_get_day_number( $some_date )
{

	// Check if date is valid
	$returnCode = isValidDate($some_date);
	if ( $returnCode == 1 ) {
		return '';
		//return '*ERROR';
	}

	// Create date object
	$some_date_object = date_create($some_date);

	//$wrk_day = date_format($some_date_object,'N');
	$gay_number = date_format($some_date_object,'w');


	return $gay_number;

}

//======================================================================================
// Function: Get day number ISO
//======================================================================================
function REEFT_get_day_number_ISO( $some_date )
{

	// Check if date is valid
	$returnCode = isValidDate($some_date);
	if ( $returnCode == 1 ) {
		return '';
		//return '*ERROR';
	}

	// Create date object
	$some_date_object = date_create($some_date);

	$gay_number = date_format($some_date_object,'N');


	return $gay_number;

}


//======================================================================================
// Check if valid date - might not be 100% waterproof, but it will do...
//======================================================================================
function isValidDate($date, $format = 'Y-m-d H:i:s' ) {

	$returnCode = 0;

	try {
		$dateTimeObject = new DateTime($date);
	} catch (Exception $exc) {
		$returnCode = 1;
	}
	
	return $returnCode;

}

//======================================================================================
// Check if valid date - might not be 100% waterproof, but it will do...
//======================================================================================
function isValidDateNew($date, $format = 'Y-m-d H:i:s' ) {

	$returnCode = 0;
	

	try {
		$dateTimeObject = new DateTime($date);
	} catch (Exception $exc) {
		$returnCode = 1;
		return $returnCode;
	}
	

	if ( $returnCode == 0 )	 {
		
		$myDate 	= explode(' ', $date);
		$tempDate 	= explode('-', $myDate[0]);
		
		$elm = count($tempDate);
		
		if ( $elm <> 3 ) {
			$returnCode = 1;
			return $returnCode;
		}
		

		// checkdate(month, day, year)
		if ( !checkdate($tempDate[1], $tempDate[2], $tempDate[0]) ) {
			$returnCode = 1;
		}
	
		$tempTime 	= explode(':', $myDate[1]);
		
		
		$elm = count($tempTime);
		
		if ( $elm <> 3 ) {
			$returnCode = 1;
			return $returnCode;
		}
		
		if ( $tempTime[0] > 23 ) {
			$returnCode = 1;
			return $returnCode;
		}
		
		if ( $tempTime[1] > 59 ) {
			$returnCode = 1;
			return $returnCode;
		}
		
		if ( $tempTime[2] > 59 ) {
			$returnCode = 1;
			return $returnCode;
		}
		
	
		
	}

	return $returnCode;

}


?>